﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace 默写
{
    public class ALesson
    {
        /// <summary>
        /// 构建函数
        /// </summary>
        public ALesson(int LessonNumber, ALessonType type)
        {
            Number = LessonNumber;
            Type = type;
            ImageIndex = ImageIndex.Lesson;
            Characters = new List<ACharacter>();
        }

        /// <summary>
        /// 构建函数
        /// </summary>
        public ALesson(int LessonNumber)
        {
            Number = LessonNumber;
            Type = ALessonType.课文;
            ImageIndex = ImageIndex.Lesson;
            Characters = new List<ACharacter>();
        }

        /// <summary>
        /// 课文编号
        /// </summary>
        /// <value>&gt;1</value>
        public int Number
        {
            get;
            set;
        }

        /// <summary>
        /// 识字或者课文的类型
        /// </summary>
        public ALessonType Type
        {
            get;
            set;
        }

        /// <summary>
        /// 生字
        /// </summary>
        public List<ACharacter> Characters
        {
            get;
            set;
        }

        /// <summary>
        /// 课文图片索引值
        /// </summary>
        /// <value>2</value>
        public ImageIndex ImageIndex
        {
            get;
            set;
        }

        /// <summary>
        /// 生成TreeNode节点
        /// </summary>
        public TreeNodeExt ToNode()
        {
            TreeNodeExt rtnTN = new TreeNodeExt(Type.ToString() + Number, (int)ImageIndex, (int)ImageIndex);
            rtnTN.Name = Type.ToString() + ":" + Number;
            foreach (ACharacter acharacter in Characters)
            {
                rtnTN.Nodes.Add(acharacter.ToNode());
            }
            return rtnTN;
        }

        /// <summary>
        /// 生成课文所有内容的字符串
        /// </summary>
        public override String ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(Type.ToString()+Number);
            sb.Append("\n");
            foreach (ACharacter acharacter in Characters)
            {
                sb.Append(acharacter.ToString());
                sb.Append("\n");
            }
            return sb.ToString();
        }

        /// <summary>
        /// 生成pdf格式内容
        /// </summary>
        public Phrase ToPDF()
        {
            Phrase phrase = new Phrase();
            string fontpath = Environment.GetFolderPath(Environment.SpecialFolder.Fonts);
            BaseFont weiBF = BaseFont.CreateFont(fontpath + @"\STXINWEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            Font weifont = new Font(weiBF, 22);
            Chunk textchun = new Chunk(Type.ToString() + Number, weifont);
            phrase.Add(textchun);
            phrase.Add(Chunk.NEWLINE);
            foreach (ACharacter acharacter in Characters)
            {
                phrase.Leading = 24;
                phrase.Add(acharacter.ToPDF());
            }
            return phrase;

        }

        /// <summary>
        /// 由节点生成ALesson对象
        /// </summary>
        public static ALesson FromNode(TreeNodeExt anode)
        {
            String[] strarray = anode.Name.Split(':');
            int lessonnumber = Convert.ToInt16(strarray[1]);
            ALessonType lessontype = (ALessonType)Enum.Parse(typeof(ALessonType), strarray[0], true);
            ALesson alesson = new ALesson(lessonnumber, lessontype);
            foreach (TreeNodeExt nodeext in anode.Nodes)
            {
                alesson.Characters.Add(ACharacter.FromNode(nodeext));
            }
            return alesson;
        }

        ///// <summary>
        ///// 由字符串加入生字
        ///// </summary>
        //public void AddCharacters(ACharacterRequest request,string character, params string[] words)
        //{
        //    ACharacter acharacter = new ACharacter(character, request);
        //    foreach (string aword in words) {
        //        acharacter.Words.Add(new AWord(aword));
        //    }
        //    Characters.Add(acharacter);
        //}

        ///// <summary>
        ///// 由字符串加入生字
        ///// 0:认或写
        ///// 1:生字
        ///// 2:词组
        ///// 分隔符':'
        ///// </summary>
        //public void AddCharacters(string str)
        //{
        //    string[] strarray = str.Replace("：",":").Replace(" ","").Split(':');
        //    if ((strarray[0] == "认" || strarray[0] == "写") && (strarray.Count() == 3))
        //    {
        //        ACharacterRequest RorW = (ACharacterRequest)Enum.Parse(typeof(ACharacterRequest), strarray[0], true);
        //        string charactertext = strarray[1];
        //        string[] wordarray = strarray[2].Replace("；", ";").Split(';');
        //        AddCharacters(RorW, charactertext, wordarray);
        //    }
        //}

        /// <summary>
        /// 由字符串生成ALesson对象
        /// </summary>
        /// <param name="str">输入字符串：格式为
        /// 0:识字或课文
        /// 1:第几课
        /// 2:认或写
        /// 3:生字
        /// 4:词组</param>
        /// <returns>ALesson对象</returns>
        public static ALesson FromString(params string[] strarray)
        {
            if ((strarray[0] == "识字" || strarray[0] == "课文") && (strarray.Count() >= 4))
            {
                int lessonnumber = Convert.ToInt16(strarray[1]);
                ALessonType lessontype = (ALessonType)Enum.Parse(typeof(ALessonType), strarray[0], true);
                ALesson alesson = new ALesson(lessonnumber, lessontype);
                string[] temparray = null;
                Array.Copy(strarray, temparray, strarray.Length - 2);
                ACharacter acharacter = ACharacter.FromString(temparray);
                if (acharacter != null)
                {
                    alesson.Characters.Add(acharacter);
                }
                return alesson;
            }
            else {
                return null;
            }

        }
    }
}
